﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineCellphoneStore.UserPages
{
    public partial class PreviousPurchase : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserContext();
                ShowOrders();
            }
        }
        protected void OrdersGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Getting OrderId from selection
            SpecificOrderDiv.Attributes.CssStyle.Add("display", "inline");
            int Index = Int32.Parse(e.CommandArgument.ToString());
            GridViewRow row = OrdersGridView.Rows[Index];
            int OrderId = Int32.Parse(row.Cells[1].Text.ToString());
            // Display the order
            ShowSpecificOrder(OrderId);
        }
        private void ShowOrders()
        {
            try
            {
                SpecificOrderDiv.Attributes.CssStyle.Add("display", "none");
                DAL.DBAdapter.OrdersDataTable dt = SQLAdapter.GetAllClientOrders(Int32.Parse(Session["ClientID"].ToString()));
                if (dt.Count == 0)
                {
                    PrevOrdersHeader.InnerHtml = "You havn't commit any orders yet.";
                }
                else
                {
                    PrevOrdersHeader.InnerHtml = "Your Orders so far";
                    dt.Columns.Remove("ClientID");
                    OrdersGridView.Attributes.CssStyle.Add("text-align", "center");
                    OrdersGridView.DataSource = dt;
                    OrdersGridView.DataBind();
                    OrdersGridView.Attributes.Add("class", "table table-borderd");
                    OrdersGridView.HeaderRow.Cells[1].Text = "Order Number";
                    OrdersGridView.HeaderRow.Cells[2].Text = "Order Date";
                    OrdersGridView.HeaderRow.Cells[3].Text = "Shipment Date";
                    OrdersGridView.HeaderRow.Cells[4].Text = "Currency";
                    OrdersGridView.HeaderRow.Cells[5].Text = "Total Amount";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        private void ShowSpecificOrder(int OrderId)
        {
            try
            {
                DAL.DBAdapter.CellphoneInOrderDataTable dt = SQLAdapter.GetCellphonesInOrder(OrderId);
                SpecificOrderGridView.Attributes.CssStyle.Add("text-align", "center");
                SpecificOrderGridView.DataSource = dt;
                SpecificOrderGridView.DataBind();
                OrderNumberHeader.InnerHtml = "Order Number: " + OrderId;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }
        private void UserContext()
        {
            HandleUserLables();
        }
        private void HandleUserLables()
        {
            // Update labels
            aHelloUser.InnerHtml += Session["UserName"].ToString();
            aLastEntered.InnerHtml += Session["LastVisit"].ToString();
        }
        protected void LogOut_Click(object sender, EventArgs e)
        {
            string Url = "../Main.aspx?LogOut=true";
            Response.Redirect(Url, false);
        }
        protected void BtnBackToMain_Click(object sender, EventArgs e)
        {
            string Url = "../Main.aspx";
            Response.Redirect(Url, false);

        }
    }
}